<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnfm.php");
	$MAIN = new MAIN();
}
$MAIN->users->checkLogin(true);

if(isset($_POST['txt_name'])){
	if($MAIN->groups->setGroup($_POST['txt_group_id'],$_POST)){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=groups&view=all';}).delay(3000);</script>
	<?php
	};
	exit();
}
if(isset($_REQUEST['del'])){
	if($MAIN->groups->delGroup($_REQUEST['del'])){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=groups&view=all';}).delay(3000);</script>
	<?php	
	};
	exit();
}
?>
<div class="activ">
<?php
switch(@$_REQUEST['view']){
	default:
	case 'all':
		$groups = $MAIN->groups->getGroups();
	?>
	<h1>Alla grupper i systemet</h1>
	<table cellpadding="0" cellspacing="0" class="list">
	<tr>
		<th>ID</th>
		<th style="width:10px;"></th>
		<th>Grupp namn</th>
		<th style="width:10px;"></th>
		<th>Nivå</th>
		<th style="width:10px;"></th>
		<th>Grupp beskrivning</th>
		<th style="width:10px;"></th>
		<th>Administrera</th>
	</tr>
	<?php
	foreach($groups as $group){
	?>
	<tr class="entry">
		<td><?php echo($group['id']);?></td>
		<td></td>
		<td><?php echo($group['display_name']);?></td>
		<td></td>
		<td><?php echo($group['rights']);?></td>
		<td></td>
		<td><?php echo($MAIN->stringLength($group['desc'], 30));?>...</td>
		<td></td>
		<td class="tools">			
			<a href="javascript:void(0);" onclick="delGroup(<?php echo($group['id']);?>)"><img src="images/btn_admin_del.png" alt="Ta Bort" title="Ta Bort"/></a>
			<a href="?cat=groups&amp;view=edit&amp;id=<?php echo($group['id']);?>"><img src="images/btn_admin_edit.png" alt="Redigera grupp" title="Redigera grupp"/></a>
		</td>
	</tr>
	<?php
	}
	?>
	</table>
	<script type="text/javascript">
		var delGroup = function(id){
			var msg='Bekräfta borttagning';
			if(confirm(msg)){
			ajaxGet('groups.php','del='+id);
			}
		}
	</script>
	<?php
	break;
	case 'edit':
	case 'add':
		$group=$MAIN->groups->getGroup(@$_REQUEST['id']);
	?>
	<h1>Skapa grup</h1>
	<form name="frm_group" id="frm_group" action="groups.php" method="post" onsubmit="return false;">
		<input type="hidden" name="txt_group_id" value="<?php echo($group['id']);?>"/>
	<table cellpadding="0" cellspacing="0">
		<tr>
			<td>Grupp namn</td>
			<td><input type="text" name="txt_name" value="<?php echo($group['name']);?>"/></td>
		</tr>
		<tr>
			<td>Grupp visningnamn</td>
			<td><input type="text" name="txt_display_name" value="<?php echo($group['display_name']);?>"/></td>
		</tr>
		<tr>
			<td>Beskrivning</td>
			<td><textarea name="txt_desc" rows="" cols=""><?php echo($group['desc']);?></textarea></td>
		</tr>
		<tr>
			<td>Nivå (rättigheter)</td>
			<td>
				<select name="ddl_rights">
					<?php
					for($i=0; $i<=100;$i+=10){
						$disabled="";
						if($MAIN->currentUser['grouplevel']>=$i && $MAIN->currentUser['grouplevel']!=0){
							$disabled = "disabled=\"disabled\" ";
						}
						$groupName=$groupDesc="";
						$g = $MAIN->groups->getGroupByRights($i);						
						if($g['name']!=""){
							$groupName = " - ".$g['display_name'];
							$groupDesc = $g['desc'];
						}
					?>
					<option <?php echo($disabled);?>alt="<?php echo($groupDesc);?>" title="<?php echo($groupDesc);?>" value="<?php echo($i);?>" <?php if($group['rights']==$i){?> selected="selected"<?php }?>><?php echo($i.$groupName);?></option>
					<?php
					}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" style="text-align:right;">
				<input type="submit" name="btn_submit" value="Spara" onclick="sendForm('frm_group');"/>
			</td>
		</tr>
	</table>
	</form>
	<?php
	break;
}
?>
</div>